import 'package:flutter/material.dart';
import './Products.dart';
import 'package:scoped_model/scoped_model.dart';
import '../../ScopedModels/ScopedMain.dart';
// import './Product_Admin.dart';
import 'package:scoped_model/scoped_model.dart';
import '../../ScopedModels/ScopedMain.dart';

class ProductPage extends StatefulWidget {
  final ScopedMain model;
  ProductPage(this.model);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ProductPageState();
  }
}

class _ProductPageState extends State<ProductPage> {
  @override
  void initState() {
    widget.model.getHomeData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    print('[Products Widget] build');
    // TODO: implement build
    return Scaffold(
      drawer: Drawer(
          child: Column(
        children: <Widget>[
          AppBar(
            automaticallyImplyLeading: false,
            title: Text('Choose'),
          ),
          ListTile(
            title: Text('Manager Product'),
            onTap: () {
              //跳转到 '/'路由
              // Navigator.pushReplacementNamed(context, '/admin');
            },
          )
        ],
      )),
      appBar: AppBar(
        title: Text("Widget基础练习"),
        actions: <Widget>[
          ScopedModelDescendant<ScopedMain>(
              builder: (BuildContext context, Widget child, ScopedMain model) {
            return IconButton(
                icon: Icon(model.showFavorite
                    ? Icons.favorite
                    : Icons.favorite_border),
                onPressed: () {
                  model.showFavoriteProduct();
                });
          })
        ],
      ),
      body: _buildProduct(),
    );
  }

  Widget _buildProduct() {
    return ScopedModelDescendant(
        builder: (BuildContext context, Widget child, ScopedMain model) {
      Widget content = Center(child: Text('no data'));
      if (model.banners.length > 0 && !model.homeLoadingStatus) {
        content = Products();
      } else {
        content = Center(child:CircularProgressIndicator());
      }
      return RefreshIndicator(child: content, onRefresh: model.getHomeData,);
    });
  }
}
